﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace Pavilion.DesignerTool
{
    public class TrackedLock
    {
        private ResourceItem parentResource;
        private NodeScript parentNodeScript;
        private ContainerNode parentContainer;
        private Link parentLink;
        private ILock lockItem;

        private Dictionary<string, object> originalPropertyList;

        public TrackedLock(ResourceItem parentResource, NodeScript parentNodeScript, ContainerNode parentContainer, Link parentLink, ILock lockItem)
        {
            this.parentResource = parentResource;
            this.parentNodeScript = parentNodeScript;
            this.parentContainer = parentContainer;
            this.parentLink = parentLink;
            this.lockItem = lockItem;

            originalPropertyList = LockPropertyItem.GetLockProperties(lockItem);
        }

        public void TransferData()
        {
            PropertyDescriptorCollection propertyDescriptors = TypeDescriptor.GetProperties(lockItem);

            foreach (PropertyDescriptor descriptor in propertyDescriptors)
            {
                string originalValue = "";
                string propertyValue = "";

                originalValue = (originalPropertyList[descriptor.DisplayName] != null) ? originalPropertyList[descriptor.DisplayName].ToString() : "";
                propertyValue = (descriptor.GetValue(lockItem) != null) ? descriptor.GetValue(lockItem).ToString() : "";

                if (originalValue != propertyValue)
                    LockTransfer.Update(parentResource.Name, parentNodeScript.Name, parentContainer, parentLink, lockItem, descriptor);
            }
        }
    }
}
